﻿using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Reader.Libs.PRC;
using System;
using System.IO;
using System.Text;

namespace Reader.Libs.CONVERTER
{
    public class HtmlConverter
    {
        public void ConvertToTxt(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatWebPages);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatUnicodeText, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToDoc(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatWebPages);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatDocument97, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToDocX(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatWebPages);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatDocumentDefault, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToPdf(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatWebPages);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatPDF, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToMobi(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                string result="";
                using (StreamReader reader = new StreamReader(SourcePath))
                {
                    result = reader.ReadToEnd();
                    reader.Dispose();
                }

                MobiDoc md = new MobiDoc();
                md.BookContent = result;
                md.writeContent(FileToSave);
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }
    }
}
